package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbReleaseNodeDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbReleaseNodeEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbReleaseNode;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.IReleaseNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-27 15:18
 **/

@Service
public class ReleaseNodeService implements IReleaseNodeService {
    @Autowired
    TbReleaseNodeDao tbReleaseNodeDao;

    @Override
    public int save(TbReleaseNode record) {
        return tbReleaseNodeDao.insertSelective(record);
    }

    @Override
    public int update(TbReleaseNode record) {
        return tbReleaseNodeDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbReleaseNode get(Long id) {
        TbReleaseNode entity = tbReleaseNodeDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbReleaseNode item = new TbReleaseNode();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbReleaseNode> getList(TbReleaseNode where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbReleaseNodeDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbReleaseNode where, TbCustomWhere customWhere) {
        return tbReleaseNodeDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbReleaseNode selectOne(TbReleaseNode where, TbCustomWhere customWhere) {
        List<TbReleaseNode> itemList = tbReleaseNodeDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public int insertList(List<TbReleaseNode> recordList) {
        return tbReleaseNodeDao.insertList(recordList);
    }


    @Override
    public int updatePushState(Long id, TbReleaseNodeEnum.PUSH_STATE state) {
        TbReleaseNode record = new TbReleaseNode();
        record.setId(id);
        record.setPushState(state.code);
        return tbReleaseNodeDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<TbReleaseNode> getListByReleaseTaskId(Long releaseTaskId) {
        TbReleaseNode tbReleaseNodeWhere = new TbReleaseNode();
        tbReleaseNodeWhere.setReleaseTaskId(releaseTaskId);
        tbReleaseNodeWhere.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        return getList(tbReleaseNodeWhere, null, TbPageWhere.newPage(0, Integer.MAX_VALUE));
    }
}
